/**
 * 
 */
package com.meetingshow.meeting.uiservice;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.validation.groups.Default;

import org.apache.log4j.Logger;

import com.meetingshow.common.base.service.model.QueryResults;
import com.meetingshow.common.base.uiservice.BaseUIServiceImp;
import com.meetingshow.common.base.uiservice.exception.BaseUIServiceException;
import com.meetingshow.common.validator.RemoveValidator;
import com.meetingshow.common.validator.UpdateValidator;
import com.meetingshow.meeting.dao.model.MeetingTypePO;
import com.meetingshow.meeting.service.IMeetingTypeService;
import com.meetingshow.meeting.service.model.MeetingTypeQueryParas;
import com.meetingshow.meeting.uiservice.model.MeetingTypeQueryParasVO;
import com.meetingshow.meeting.uiservice.model.MeetingTypeVO;
import com.meetingshow.utils.BaseUtil;

/**
 * @author louyi
 *
 */
public class UIMeetingTypeServiceImp extends BaseUIServiceImp implements IUIMeetingTypeService {
    
    public final static Logger logger = Logger.getLogger(UIMeetingTypeServiceImp.class);

    private IMeetingTypeService meetingTypeService;
    
    /**
     * @return the meetingTypeService
     */
    public IMeetingTypeService getMeetingTypeService() {
        return meetingTypeService;
    }

    /**
     * @param meetingTypeService the meetingTypeService to set
     */
    public void setMeetingTypeService(IMeetingTypeService meetingTypeService) {
        this.meetingTypeService = meetingTypeService;
    }

    @Override
    public Integer saveMeetingType(MeetingTypeVO meetingTypeVO)
            throws BaseUIServiceException {
        
        if (!BaseUtil.objectNotNull(meetingTypeVO)) {
            this.throwValidaterException("meetingTypeVO is null.");
        }
        
        meetingTypeVO.setCreateTime(new Date());
        
        validateThrowEx(meetingTypeVO);
        
        MeetingTypeQueryParas meetingTypeQueryParas = new MeetingTypeQueryParas();
        meetingTypeQueryParas.setTypeName_e(meetingTypeVO.getTypeName());
        
        QueryResults<MeetingTypePO> mtQueryResults = null;
        
        try {
            mtQueryResults = getMeetingTypeService().queryMeetingType(meetingTypeQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (QueryResults.resultsListNotNull(mtQueryResults)) {
            logger.error("typeName [ " + meetingTypeVO.getTypeName() + " ] exists.");
            throw new BaseUIServiceException("typeName [ " + meetingTypeVO.getTypeName() + " ] exists.");
        }
        
        try {
            Integer typeId = getMeetingTypeService().saveMeetingType(meetingTypeVO.toMeetingTypePO());
            meetingTypeVO.setTypeId(typeId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        return meetingTypeVO.getTypeId();
    }

    @Override
    public void updateMeetingType(MeetingTypeVO meetingTypeVO)
            throws BaseUIServiceException {
        if (!BaseUtil.objectNotNull(meetingTypeVO)) {
            this.throwValidaterException("meetingTypeVO is null.");
        }
        
        validateThrowEx(meetingTypeVO, Default.class, UpdateValidator.class);
        
        MeetingTypeQueryParas meetingTypeQueryParas = new MeetingTypeQueryParas();
        meetingTypeQueryParas.setTypeId_e(String.valueOf(meetingTypeVO.getTypeId()));
        
        QueryResults<MeetingTypePO> meetingTypeQueryResults = null;
        try {
            meetingTypeQueryResults = getMeetingTypeService().queryMeetingType(meetingTypeQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (QueryResults.resultsListNotNull(meetingTypeQueryResults)) {
            MeetingTypePO persistMeetingTypePO = meetingTypeQueryResults.getResultList().get(0);
            persistMeetingTypePO.setTypeName(meetingTypeVO.getTypeName());
            
            //验证typeName是否重复
            meetingTypeQueryParas = new MeetingTypeQueryParas();
            meetingTypeQueryParas.setTypeName_e(meetingTypeVO.getTypeName());
            
            meetingTypeQueryResults = null;
            try {
                meetingTypeQueryResults = getMeetingTypeService().queryMeetingType(meetingTypeQueryParas);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                throw new BaseUIServiceException(e);
            }
            
            if (QueryResults.resultsListNotNull(meetingTypeQueryResults)
                    && !meetingTypeVO.getTypeId().equals(
                            meetingTypeQueryResults.getResultList().get(0).getTypeId())) {
                logger.error("typeName [ " + meetingTypeVO.getTypeName() + " ] exists.");
                throw new BaseUIServiceException("typeName [ " + meetingTypeVO.getTypeName() + " ] exists.");
            }
            
            try {
                getMeetingTypeService().updateMeetingType(persistMeetingTypePO);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                throw new BaseUIServiceException(e);
            }
        }
    }

    @Override
    public void removeMeetingType(MeetingTypeVO meetingTypeVO)
            throws BaseUIServiceException {
        if (!BaseUtil.objectNotNull(meetingTypeVO)) {
            this.throwValidaterException("meetingTypeVO is null.");
        }
        
        validateThrowEx(meetingTypeVO, RemoveValidator.class);
        
        MeetingTypeQueryParas meetingTypeQueryParas = new MeetingTypeQueryParas();
        meetingTypeQueryParas.setTypeId_e(String.valueOf(meetingTypeVO.getTypeId()));
        
        QueryResults<MeetingTypePO> meetingTypeQueryResults = null;
        try {
            meetingTypeQueryResults = getMeetingTypeService().queryMeetingType(meetingTypeQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (QueryResults.resultsListNotNull(meetingTypeQueryResults)) {
            MeetingTypePO persistMeetingTypePO = meetingTypeQueryResults.getResultList().get(0);
            try {
                getMeetingTypeService().removeMeetingType(persistMeetingTypePO);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                throw new BaseUIServiceException(e);
            }
        }
    }

    @Override
    public MeetingTypeVO loadMeetingTypeById(Integer meetingTypeId)
            throws BaseUIServiceException {
        if (meetingTypeId == null)
            this.throwValidaterException("meetingTypeId is null.");
        
        MeetingTypePO meetingTypePO = null;
        try {
            meetingTypePO = getMeetingTypeService().loadMeetingTypeById(meetingTypeId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (meetingTypePO == null)
            return null;
        else
            return new MeetingTypeVO(meetingTypePO);
    }
    
    @Override
    public QueryResults<MeetingTypeVO> queryMeetingType(
            MeetingTypeQueryParasVO meetingTypeQueryParasVO)
            throws BaseUIServiceException {
        if (!BaseUtil.objectNotNull(meetingTypeQueryParasVO)) {
            this.throwValidaterException("meetingTypeQueryParasVO is null.");
        }
        
        validateThrowEx(meetingTypeQueryParasVO);
        
        QueryResults<MeetingTypePO> meetingTypePOQueryResults = null;
        try {
            meetingTypePOQueryResults = getMeetingTypeService().queryMeetingType(meetingTypeQueryParasVO.toMeetingTypeQueryParas());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        QueryResults<MeetingTypeVO> meetingTypeVOQueryResults = new QueryResults<MeetingTypeVO>();
        List<MeetingTypeVO> meetingTypeVOList = new ArrayList<MeetingTypeVO>();
        if (QueryResults.resultsListNotNull(meetingTypePOQueryResults)) {
            for (MeetingTypePO meetingTypePO : meetingTypePOQueryResults.getResultList()) {
                if (BaseUtil.objectNotNull(meetingTypePO)) {
                    meetingTypeVOList.add(new MeetingTypeVO(meetingTypePO));
                }
            }
        }
        
        meetingTypeVOQueryResults.setFirstResultNo(meetingTypePOQueryResults.getFirstResultNo());
        meetingTypeVOQueryResults.setResultsPerGroup(meetingTypePOQueryResults.getResultsPerGroup());
        meetingTypeVOQueryResults.setResultList(meetingTypeVOList);
        meetingTypeVOQueryResults.setTotalResultsNum(meetingTypePOQueryResults.getTotalResultsNum());
        
        return meetingTypeVOQueryResults;
    }

}
